package com.gams.common;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 文件上传工具类
 *
 * @author ruoyi
 */
public class FileUploadUtils
{

    /**
     * 文件上传
     *
     * @param filePath 相对应用的基目录
     * @param file    上传的文件
     * @return 返回上传成功的文件名
     */
    public static final String upload(String filePath, MultipartFile file)
            throws IOException
    {
        String fileName = DateUtils.datePath() + "/" + file.getOriginalFilename();
        File desc = getAbsoluteFile(filePath, fileName);
        file.transferTo(desc);
        String pathFileName = filePath + "/" + fileName;
        return pathFileName;
    }


    public static File getAbsoluteFile(String uploadDir, String fileName)
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc;
    }

}
